package ua.pp.parkee.t2;

import java.util.List;
import java.util.Random;

/**
 * Created by parkee on 05.06.14.
 */
public class Producer<E> implements Runnable {

    private List<E> elements;
    private CircularBuffer<E> circularBuffer;
    private Random random;

    public Producer(List<E> elements, CircularBuffer<E> circularBuffer) {
        this.elements = elements;
        this.circularBuffer = circularBuffer;
        random = new Random();
        //System.out.println("Producer initialized");
    }

    @Override
    public void run() {
        //System.out.println("Producer run()");
        for (E element : elements) {
            circularBuffer.add(element);
            //System.out.println("Produced: " + element);
            try {
                Thread.sleep(random.nextInt(100));
            } catch (InterruptedException e) {
                //Ignore
            }
        }

    }
}
